<?PHP
error_reporting(E_ERROR | E_PARSE);

class conf
{
	var $debug = false;
	var $admin = false;
	var $facebook=true;
	var $dev;
	var $sta;
	var $pro;

	
	var $admin_url='index.php?q=home.php';
	var $path;

	
	
	var $photo_dir="../media/images/";
	var $thumb_dir="../media/images/";
	
	var $video_dir="../media/videos/";
	var $absolute_path;
	var $what_page;
	//path values


	var $site_url;
	var $site_name="";
	var $page_render;


	
	//thumbnail
	var $thumb_x=80;
	var $thumb_y=80;
	
	

	// data catcher values
	var $q;
	var $qm;
	var $qk;
	var $category;
	var $command;

	var $module;
	var $key;
	var $key_string;
	var $title;
	var $script_name;
	var $controller;
	//for pagination
	var $page;
	var $hash;
	var $id;
	var $items;
	var $date_ymd;
	var $canonical;
	var $meta;
	var $chatmode=false;
	var $default_q;
	var $popout=false;
	var $play=FALSE;
	var $test_content=false;
	var $advertise=true;
	var $style;
	var $a;
	var $v;
	var $queries=0;
	var $errors;
	var $messages;
	

	
	function conf()
	{
		
		//load this to settings db
		// 
		//local
		$this->dev['url']='localhost';
		$this->dev['name']='pinoytuner';
		$this->dev['host']='localhost';
		$this->dev['user']='root';
		$this->dev['pass']='';
		$this->dev['path']='/unearth/';


		//pre-production
		$this->sta['url']='muse.pinoytuner.com';
		$this->sta['name']='zwan';
		$this->sta['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->sta['user']='pinoytuner';
		$this->sta['pass']='fellowes';
		$this->sta['path']='/';
		
		
		//content staging
		$this->t['url']='okgo.pinoytuner.com';
		$this->t['name']='zwan';
		$this->t['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->t['user']='pinoytuner';
		$this->t['pass']='fellowes';
		$this->t['path']='/';
		
		//player staging
		$this->r['url']='korn.pinoytuner.com';
		$this->p['name']='beck';
		$this->p['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->p['user']='pinoytuner';
		$this->p['pass']='fellowes';
		$this->p['path']='/';
		
		//dev staging
		$this->p['url']='beck.pinoytuner.com';
		$this->p['name']='beck';
		$this->p['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->p['user']='pinoytuner';
		$this->p['pass']='fellowes';
		$this->p['path']='/';
		
		//Chat staging
		$this->chat['url']='mraz.pinoytuner.com';
		$this->chat['name']='zwan';
		$this->chat['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->chat['user']='pinoytuner';
		$this->chat['pass']='fellowes';
		$this->chat['path']='/';

		//production
		$this->pro['url']='pinoytuner.com';
		$this->pro['name']='zwan';
		$this->pro['host']='hale.czcoe9or0e6d.ap-southeast-1.rds.amazonaws.com';
		$this->pro['user']='pinoytuner';
		$this->pro['pass']='fellowes';
		$this->pro['path']='/';
		
		$this->site_url=$_SERVER['HTTP_HOST'];
		$this->ref=$_SERVER['HTTP_REFERER'];

		//determin if youre in admin
		$p = explode("/",$this->path);
		$e = explode("/",$_SERVER['REQUEST_URI']);
		$c = count($p)-1;
		$n = count($p);

		$this->user_id=$_SESSION['user_id'];
		if($e[$c]=='admin')
		{
		
			$this->admin=true;
		}
		$this->script_name=$e[$n];
		
		$this->initialize();
		$this->cleanup();
		
		$this->page_render();
		$this->generate_meta();
	}
	function set_default_q()
	{
			
				$this->q=$this->default_q['q'];
				$this->module=$this->default_q['module'];
				$this->key_string=$this->default_q['key_string'];
				$this->qm[0]=$this->default_q[0];
				$this->qm[1]=$this->default_q[1];
				$this->qm[2]=$this->default_q[2];
	}
	
	function get_refer_q()
	{
			$_SESSION['rq']['q']=$this->q;
			$_SESSION['rq']['module']=$this->module;
			$_SESSION['rq']['key_string']=$this->key_string;
			$_SESSION['rq']['qm']=$this->qm;	
				
	}
	function set_home_q()
	{
		$this->q='';
		$this->module='';
		$this->key_string='';
		$this->qm[0]='';
		$this->qm[1]='';
		$this->qm[2]='';
	}
	function set_refer_q()
	{
			
				$this->q=$_SESSION['rq']['q'];
				$this->module=$_SESSION['rq']['module'];
				$this->key_string=$_SESSION['rq']['key_string'];
				$this->qm=$_SESSION['rq']['qm'];
				
	}
	function initialize()
	{
	
	//set timezone
	
		date_default_timezone_set('Asia/Singapore');
		
		//pick database
		if($this->site_url==$this->dev['url'] || $this->site_url=='www.'.$this->dev['url'])
		{
			$db=$this->dev;
			$this->path=$this->dev['path'];
			$this->debug=true;
			
		}
		else if($this->site_url==$this->t['url'] || $this->site_url=='www.'.$this->t['url'])
		{
			$this->test_content=true;
			$this->debug=false;
			$db=$this->t;
			$this->path=$this->t['path'];

		}
		else if($this->site_url==$this->chat['url'] || $this->site_url=='www.'.$this->chat['url'])
		{
			$this->chatmode=true;
			$this->test_content=false;
			$this->debug=false;
			$db=$this->chat;
			$this->path=$this->chat['path'];

		}
		
		else if($this->site_url==$this->sta['url'] || $this->site_url=='www.'.$this->sta['url'])
		{
		
			$this->debug=false;
			$db=$this->sta;
			$this->path=$this->sta['path'];

		}
		else if($this->site_url==$this->r['url'] || $this->site_url=='www.'.$this->r['url'])
		{

			$db=$this->r;
			$this->path=$this->r['path'];

		}
		else if($this->site_url==$this->p['url'] || $this->site_url=='www.'.$this->p['url'])
		{
				$this->facebook=true;
			$db=$this->p;
			$this->path=$this->p['path'];

		}

		
		else if($this->site_url==$this->pro['url'] || $this->site_url=='www.'.$this->pro['url']) 
		{

			$this->debug=false;
			$this->advertise=true;
			$this->facebook=true;
			$db=$this->pro;
			$this->path=$this->pro['path'];
			
		}
		//connect to db
		$Database = mysql_pconnect($db['host'], $db['user'], $db['pass']) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db ($db['name']);


		//grab settings
		$settings=get_sql('settings','setting_id',NULL,NULL);
		foreach($settings as $x)
		{
			$this->a[$x['setting_name']]=$x['setting_value'];
		}
		

		
		// process Q values
		$this->title=$this->a['page-title'];

		$this->q=$_GET['q'];
	//	$this->cmd=$_GET['cmd'];
		$this->hash=$_GET['hash'];
		$this->id=$_GET['id'];
		
		


		$e = explode("/",$this->q);
		$this->qm=$e;
	
		$qz=$e;
		
		foreach($qz as $v)
		{
			$qq=str_replace("-"," ",$v);
			$this->qk[]=$qq;
		}
		$this->qk=$qq;
		$this->module = $e[0];
		$this->command = $e[1];
		$this->key = $e[1];
		$this->cat = $e[2];
		$this->key_2 = $e[3];
		$this->key_string =	str_replace("-"," ",$e[1]);
		$this->date_ymd=date("Y-m-d");
		$v=$e;
		$this->httpcode='http://';
		if ($_SERVER['HTTPS'] == "on")
		{
			$this->httpcode='https://';
		}
		
		$this->absolute_path=$this->httpcode.$_SERVER['HTTP_HOST'].$this->path;
		$this->canonical=$this->httpcode.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		//process pagination
		
		if(empty($_GET['page']))
		{
			$this->page=1;
		}
		else
		{
			$this->page=$_GET['page'];
		}
		
		$this->items=5;
	}
	function cleanup()
	{

		$this->photo_dir=$this->a['photo-dir'];
		$this->thumb_dir=$this->a['thumb-dir'];
		$this->video_dir=$this->a['video-dir'];
	
		$this->site_name=$this->a['site-name'];
		$this->site_update=$this->a['site-update'];
		$this->default_page=$this->a['default-page'];
		$this->contact_email=$this->a['contact-email'];
		$this->thumb_x=$this->a['thumb-x'];
		$this->thumb_y=$this->a['thumb-y'];
		
		$this->default_q['q']=$this->a['default-q-q'];
		$this->default_q['module']=$this->a['default-q-module'];
		$this->default_q['key_string']=$this->a['default-q-key-string'];
		$this->default_q[0]=$this->a['default-q-0'];
		$this->default_q[1]=$this->a['default-q-1'];
		$this->default_q[2]=$this->a['default-q-2'];
	
	
	}
	
	function page_render()
	{

		$page=$this->qm[0].'.php';
	
		if(empty($this->q))
		{
			
			$this->controller='admin/m/content/c_home.php';
			$this->what_page='system.php';
			$this->page_render='admin/m/content/o_home.php';
		}
		else if(file_exists($page))
		{
	
			$this->what_page=$page;

		}
		else
		{
		
			$this->what_page='system.php';
			$this->page_render='admin/e/404.php';
		}

	}
	
	function print_messages()
	{
		
	//	$this->messages[]='Message Note';
	//	$this->errors[]='You are Stupid';
		foreach($this->errors as $x)
		{
			em($x);
		}
		foreach($this->messages as $x)
		{
			pm($x);
		}
	}
	
	function generate_meta()
	{
		$this->meta['SearchTitle']=$this->a['SearchTitle'];
		$this->meta['Description']=$this->a['Description'];
		$this->meta['Title']=$this->a['Title'];
		$this->meta['Keywords']=$this->a['Keywords'];
		$this->meta['SearchDescription']=$this->a['SearchDescription'];
		$this->meta['page_title']=$this->a['site-name'].' - '.$this->a['slogan'];
	}
	
}

$conf = new conf();


$page = new paginator;
$page->q=$conf->q;
//$page->cmd=$conf->cmd;
$page->hash=$conf->hash;
$page->id=$conf->id;

if($conf->debug)
{

	ini_set("display_errors", 1);
}
else
{

	ini_set("display_errors", 0);
}


define("photo_dir",$conf->photo_dir);
define("thumb_dir",$conf->thumb_dir);
?>
